<?php
/**
 * 《亲属情况》实现类
 *
 * @author 孙晓晔
 * @version $Id: Family.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../emp/Info.class.php');

class Family extends Info {
	public $tbl = Constant::tbl_emp_family;
	
	public $relative = array(
		'-' => '请选择',
		'父子' => '父子',
		'父女' => '父女',
		'母子' => '母子',		
		'母女' => '母女',
		'姐弟' => '姐弟',
		'姐妹' => '姐妹',
		'兄弟' => '兄弟',
		'兄妹' => '兄妹',		
		'夫妻' => '夫妻',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '正常',
		'2' => '停止',
		
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'name' => '姓名',
		'address' => '地址',
		'job' => '所在单位',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('relative', $this->relative);
	}

	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'resume/family/find');
		return true;
	}

	function orderby($px) {
		$ret = " order by id desc";
		if (empty($px) || $px == "-") {
			return $ret;
		} else {
			return ' order by '.$px.' desc';
		}

		return $ret;
	}
	
	function _where($request) {
		$emp_s = $this->_input($request['emp_s']);
		
		$where = " WHERE a.emp = b.id AND a.emp = '" . $_SESSION['id'] . "'";

		if (!empty($emp_s)) {
			$where .= " AND emp = '" . $emp_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		
		return $where;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		$fmt = "SELECT a.* FROM %s a, %s b %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, Constant::tbl_emp, $where, $orderby);
		debug($sql);		
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {

			// $row['emp_t'] = Name::emp($row['emp']);

			$ret[] = $row;
		}
		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'resume/family/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'resume/family/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$id = $_SESSION['id'];

		$name = $request['name'];

		$relative = $request['relative'];
		$address = $request['address'];
		$job = $request['job'];
		$tel = $request['tel'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(emp, name, relative, address, job, tel, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $id, $name, $relative, $address, $job, $tel);
		debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'resume/family/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$name = $request['name'];
		$relative = $request['relative'];
		$address = addslashes($request['address']);
		$job = addslashes($request['job']);
		$tel = $request['tel'];
		$status = $request['status'];

		clean($this->tbl . "_status_" . $status);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET name = '%s', relative = '%s', address = '%s', job = '%s', tel = '%s' ";
		$fmt .= ", status = '%s', modifytime = now() WHERE id = '%s' ";
		$sql = sprintf($fmt, $this->tbl, $name, $relative, $address, $job, $tel, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'resume/family/view');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		$sql = "DELETE FROM " . $this->tbl . " WHERE id " . $insql;
		// debug($sql);
		$query = $db->query($sql);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
}
?>